package com.self.match.disruptor;

import com.alibaba.fastjson.JSON;
import com.lmax.disruptor.EventHandler;
import com.self.common.param.Order;
import com.self.match.enums.MatchStrategy;
import com.self.match.model.OrderBooks;
import com.self.match.service.MatchServiceFactory;
import lombok.extern.slf4j.Slf4j;

/**
 * @version v1.0
 * @ClassName: 该对象 有多个: 和Symbol的数据对应，
 * 针对某一个OrderEventHandler ,只会同一时间有一个线程来执行它
 * @Description:
 * @Author: Mac
 * @Date: 2022/9/3 21:13
 */
@Slf4j
public class OrderEventHandler implements EventHandler<OrderEvent> {

    private OrderBooks orderBooks;

    public OrderEventHandler(OrderBooks orderBooks) {
        this.orderBooks = orderBooks;
    }

    /**
     * @Title: 接受到消息
     * @Description: 
     * @Param [orderEvent, sequence, endOfBatch]
     * @Return void
     * @Author Mac
     * @Throws
     */
    @Override
    public void onEvent(OrderEvent orderEvent, long sequence, boolean endOfBatch) throws Exception {
        log.info("开始处理订单事件===================>{}", JSON.toJSONString(orderEvent));
        Order source = (Order)orderEvent.getSource();
        // 我们接收到了一个不属于我们处理的数据,我们不处理
        if (!orderBooks.getSymbol().equals(source.getSymbol())){
            return;
        }
        //撮合
        MatchServiceFactory.getMatchService(MatchStrategy.LIMIT_PRICE).match(orderBooks,source);
    }

    public OrderBooks getOrderBooks() {
        return orderBooks;
    }
}
